#!/bin/sh
#
# see: dh_installdeb(1)

set -e

#http://software-dl.ti.com/codegen/non-esd/downloads/download.htm#C6000
package="ti-c6000-cgt-v8.1.x-installer"

#8.0.x
#ce47b4e95e31fddd69183c8b671ebf90f3ef40e3eadfc62fd9200f24406e9e51  ti_cgt_c6000_8.0.1_armlinuxa8hf_busybox_installer.sh
#f435f886610fc7df585a6ef898494e621b3bfa17f20658436f5e63af86a9a987  ti_cgt_c6000_8.0.3_armlinuxa8hf_busybox_installer.sh
#e8c3251d8a54e06ffc980dd8f9204abe0b21d68f5cdd7d0efeb69a78b3b7f77c  ti_cgt_c6000_8.0.4_armlinuxa8hf_busybox_installer.sh

#8.1.x
#286a96e3082086c37129cbbc6c3665c76028b68975d02fc30375a6aaba00532a  ti_cgt_c6000_8.1.0_armlinuxa8hf_busybox_installer.sh
#d29a93c55a55009b1aa59c79511d4b8ca5f559f81e5d5cdab79bba9e56a0a156  ti_cgt_c6000_8.1.2_armlinuxa8hf_busybox_installer.sh
#577b9dc43b9b1fb893a1d5cc6b6da149e89bd40e4c7df3dd5f4b6453b4203940  ti_cgt_c6000_8.1.3_armlinuxa8hf_busybox_installer.sh
#65f2efb9e6c941624fb25e7d7ef24eccce292823357389f8d3e327f40efc1adb  ti_cgt_c6000_8.1.4_armlinuxa8hf_busybox_installer.sh
#4c1908750daa119ac84995b16fc306fdb38253008f85f0b54201651de3a9d595  ti_cgt_c6000_8.1.5_armlinuxa8hf_busybox_installer.sh
#daf4bda765b201ed1bd4c1f9c5961e16ca0801322a98dc2f36ad9f165c8f162a  ti_cgt_c6000_8.1.6_armlinuxa8hf_busybox_installer.sh
#b095f9af5cb7c130421e2349c0280c453072bfdd0b00aaa84ce245b133d8b5e0  ti_cgt_c6000_8.1.7_armlinuxa8hf_busybox_installer.sh

#8.2.x
#13b84434e88a91316809c0377552ae2396499344ed1c5eb77f42cce1d6e2b0a8  ti_cgt_c6000_8.2.0_armlinuxa8hf_busybox_installer.sh
#7626c261ab817672d204bb009f3f7cac60edd992e59bfc30e0897ae3113445f5  ti_cgt_c6000_8.2.1_armlinuxa8hf_busybox_installer.sh
#4b2e605a4afc3d4d0ac74013ee32496541abf093aedd70bc7ef040597933b1ea  ti_cgt_c6000_8.2.2_armlinuxa8hf_busybox_installer.sh
#9601df50393a17f76c0211c488454e31271e358892c0e7bea3f7d61c347e7269  ti_cgt_c6000_8.2.3_armlinuxa8hf_busybox_installer.sh

version="8.1.7"
FILENAME="ti_cgt_c6000_${version}_armlinuxa8hf_busybox_installer.sh"
SHA256SUM_TGZ="b095f9af5cb7c130421e2349c0280c453072bfdd0b00aaa84ce245b133d8b5e0"
PARTNER_URL="http://downloads.ti.com/codegen/esd/cgt_public_sw/C6000/${version}"

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

dl_file() {
	wget --no-check-certificate $PARTNER_URL/$FILENAME
}

validdate_file() {
	sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
	if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
		rm -rf $FILENAME
		dl_file
		sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
		if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
			echo "dl failure"
			exit 1
		fi
	fi
}

dl_install() {
	if [ ! -d /var/cache/${package} ] ; then
		mkdir -p /var/cache/${package}
	fi

	cd /var/cache/${package}/

	if [ ! -f $FILENAME ] ; then
		dl_file
	fi

	if [ -f $FILENAME ] ; then
		validdate_file

		chmod +x $FILENAME
		./$FILENAME --prefix / --temp `mktemp -d`
	fi
}

case "$1" in
    configure)
	dl_install
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

